﻿using PPSMS.CommonForm;
using PPSMS.Commons;
using PPSMS.Controllers;
using PPSMS_BLL;
using PPSMS_Model;
using Sunny.UI;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PPSMS
{
    public partial class PSStatusForm : UIPage
    {
        private InverterDevices_BLL _inverterBLL = new();
        private StationStatistics_BLL stationStatistics_BLL = new();
        private StationStatus_BLL stationStatus_BLL = new();
        private Dictionary<long, InverterControl> _inverterControls = new();

        public static event Action<long> OnDeleteRefresh; 

        public static event Action<InverterDevices> OnInverterClick;

        public PSStatusForm()
        {
            InitializeComponent();
            AddInverterForm.OnInverterControlRefresh += Refresh;
            DeviceManagement.OnDeleteRefresh += (id) =>
            {
                uiFlowLayoutPanel1.Remove(_inverterControls[id]);
                _inverterControls.Remove(id);
                long count = stationStatus_BLL.SelectNewest();
                Lbl_InveterCount.Text = count.ToString();
            };
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            var stationCurrentPower = 0.0;
            if (_inverterControls.Count != 0)
            {
                foreach (var inverterControl in _inverterControls.Values)
                {
                    double currentPower = inverterControl.CurrentPower;
                    stationCurrentPower += currentPower;
                }
            }
            //公共数据更新
            dashBoard1.ActualValue = (float)stationCurrentPower;

            var v = Extension.ReadOPCValue("Channel1.逆变器1.dailyElectricityGeneration");
            v *= Count;
            Lbl_DEG.Text = v.ToString("F1");
            Lbl_MEG.Text = (v * 30).ToString("F1");
            Lbl_YEG.Text = (v * 360).ToString("F1");

            Lbl_DI.Text = (v * 0.67).ToString("F1");
            Lbl_MI.Text = (v * 30 * 0.67).ToString("F1");
            Lbl_YI.Text = (v * 360 * 0.67).ToString("F1");

            Lbl_Irradiance.Text = Extension.ReadOPCValue("PLC.设备1.Irradiance", true).ToString("F1");
            var temp = Extension.ReadOPCValue("PLC.设备1.Temp", true);
            var humi = Extension.ReadOPCValue("PLC.设备1.Humi", true);
            Lbl_TH.Text = $"{temp:F1}℃/{humi:F1}%";
            Lbl_Wind.Text = Extension.ReadOPCValue("PLC.设备1.WindSpeed", isPLC: true).ToString("F1");
            
        }
        private static long Count = 0;
        private void PSStatusForm_Load(object sender, EventArgs e)
        {
            double RatedPower = 0;//峰值发电功率
            double ModulePeakPower = 0;//模块峰值功率
            var inverterDevices = _inverterBLL.SelectAllDevices();
            foreach (var inverter in inverterDevices)
            {
                RatedPower += double.Parse(inverter.RatedPower);
                ModulePeakPower += double.Parse(inverter.Module_Peak_Power);
                if (_inverterControls.ContainsKey(inverter.InverterID))
                {
                    continue;
                }
                else
                {
                    var inverterControl = new InverterControl(inverter);
                    inverterControl.OnDataReceived += () =>
                    {
                        inverterControl.CurrentPower = Extension.ReadOPCValue($"Channel1.逆变器{inverterControl.InverterID}.currentPowerGeneration");
                        inverterControl.DEG = Extension.ReadOPCValue($"Channel1.逆变器{inverterControl.InverterID}.dailyElectricityGeneration");
                    };
                    inverterControl.OnDetail_Click += (i) =>
                    {
                        OnInverterClick?.Invoke(i);
                    };
                    _inverterControls.Add(inverter.InverterID, inverterControl);
                    uiFlowLayoutPanel1.Controls.Add(inverterControl);
                }
            }
            Lbl_TotalTopPower.Text = RatedPower.ToString();
            dashBoard1.EndValue = (float)ModulePeakPower;
            Count = stationStatus_BLL.SelectNewest();
            Lbl_InveterCount.Text = Count.ToString();
        }

        /// <summary>
        /// 逆变器列表刷新事件
        /// </summary>
        /// <param name="control"></param>
        private void Refresh(InverterControl control)
        {
            if (!_inverterControls.ContainsKey((long)control.InverterID))
            {
                _inverterControls.Add((long)control.InverterID, control);
                long count = stationStatus_BLL.SelectNewest();
                Lbl_InveterCount.Text = count.ToString();
            }
            else
            {
                uiFlowLayoutPanel1.Remove(_inverterControls[(long)control.InverterID]);
                _inverterControls.Remove((long)control.InverterID);
                _inverterControls.Add((long)control.InverterID, control);
            }
            uiFlowLayoutPanel1.Add(control);
            control.OnDataReceived += () =>
            {
                control.CurrentPower = Extension.ReadOPCValue($"Channel1.逆变器{(long)control.InverterID}.currentPowerGeneration");
                control.DEG = Extension.ReadOPCValue($"Channel1.逆变器{(long)control.InverterID}.dailyElectricityGeneration");
            };
            control.OnDetail_Click += (i) =>
            {
                OnInverterClick?.Invoke(i);
            };
           
        }

    }
}
